package ytdp.careagle.com.ytdp.activity;

import android.text.Editable;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.umeng.analytics.dplus.UMADplus;

import butterknife.Bind;
import butterknife.OnClick;
import ytdp.careagle.com.ytdp.R;
import ytdp.careagle.com.ytdp.app.APP;
import ytdp.careagle.com.ytdp.mvp.presenter.InputPhonePresenter;
import ytdp.careagle.com.ytdp.mvp.view.IInputPhoneView;
import ytdp.careagle.com.ytdp.myview.MyEditText;
import ytdp.careagle.com.ytdp.utils.SystemUtil;

public class InputPhoneActivity extends BaseActivity implements IInputPhoneView {

    @Bind(R.id.et)
    MyEditText edittext;
    @Bind(R.id.number1)
    ImageView number1;
    @Bind(R.id.number2)
    ImageView number2;
    @Bind(R.id.number3)
    ImageView number3;
    @Bind(R.id.number_delete)
    ImageView numberDelete;
    @Bind(R.id.number4)
    ImageView number4;
    @Bind(R.id.number5)
    ImageView number5;
    @Bind(R.id.number6)
    ImageView number6;
    @Bind(R.id.number0)
    ImageView number0;
    @Bind(R.id.number7)
    ImageView number7;
    @Bind(R.id.number8)
    ImageView number8;
    @Bind(R.id.number9)
    ImageView number9;
    @Bind(R.id.number_clear)
    ImageView numberClear;
    @Bind(R.id.tv_input_ok)
    TextView tvInputOk;
    @Bind(R.id.tv_close)
    TextView tv_close;
    @Bind(R.id.activity_main)
    RelativeLayout activityMain;
    private InputPhonePresenter presenter;
    private String valuationQueryRecordIds;

    @Override
    public int getLayoutId() {
        return R.layout.activity_input_phone;
    }

    @Override
    public void initView() {
        UMADplus.track(this, "保存用户信息页面", null);
        valuationQueryRecordIds = getIntent().getStringExtra("valuationQueryRecordIds");
        presenter = new InputPhonePresenter(this);
    }

    @OnClick({R.id.number0, R.id.number1, R.id.number2, R.id.number3, R.id.number4,
            R.id.number5, R.id.number6, R.id.number7, R.id.number8,
            R.id.number9, R.id.number_clear, R.id.number_delete, R.id.tv_input_ok, R.id.tv_close})
    public void onClick(View view) {
        APP.getInstance().resetCountDown();
        Editable editable = edittext.getText();
        int start = edittext.getSelectionStart();
        int end = edittext.getSelectionEnd();
        switch (view.getId()) {
            case R.id.number0:
                editable.insert(start, "0");
                break;
            case R.id.number1:
                editable.insert(start, "1");
                break;
            case R.id.number2:
                editable.insert(start, "2");
                break;
            case R.id.number3:
                editable.insert(start, "3");
                break;
            case R.id.number4:
                editable.insert(start, "4");
                break;
            case R.id.number5:
                editable.insert(start, "5");
                break;
            case R.id.number6:
                editable.insert(start, "6");
                break;
            case R.id.number7:
                editable.insert(start, "7");
                break;
            case R.id.number8:
                editable.insert(start, "8");
                break;
            case R.id.number9:
                editable.insert(start, "9");
                break;
            case R.id.number_delete:
                if (editable != null && start > 0) {
                    editable.delete(start - 1, start);
                } else if (editable != null && start != end) { // delete selection
                    editable.delete(start, end);
                }
                break;
            case R.id.number_clear:
                editable.clear();
                break;
            case R.id.tv_input_ok:
                presenter.inputPhone(getWContext().get());
                break;
            case R.id.tv_close:
                finish();
                break;
        }
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
    }

    @Override
    public String getPhone() {
        return edittext.getText().toString();
    }

    @Override
    public String getValuationQueryId() {
        return valuationQueryRecordIds;
    }

    @Override
    public void saveSuccess() {
        getOperation().forward(SaveCustomerInfoActivity.class);
        finish();
    }
}
